package com.huashi.dealer.commonEntity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单模型类
 * 对应数据库表 yoshop_order
 */
@Data
@TableName("yoshop_order")
@Schema(description = "订单实体")
public class Order{

    /**
     * 订单ID
     */
    @TableId(value = "order_id", type = IdType.AUTO)
    @Schema(description = "订单ID")
    private Integer orderId;

    /**
     * 订单号
     */
    @TableField("order_no")
    @Schema(description = "订单号")
    private String orderNo;

    /**
     * 商品总金额(不含优惠折扣)
     */
    @TableField("total_price")
    @Schema(description = "商品总金额(不含优惠折扣)")
    private BigDecimal totalPrice;

    /**
     * 订单金额(含优惠折扣)
     */
    @TableField("order_price")
    @Schema(description = "订单金额(含优惠折扣)")
    private BigDecimal orderPrice;

    /**
     * 优惠券ID
     */
    @TableField("coupon_id")
    @Schema(description = "优惠券ID")
    private Integer couponId;

    /**
     * 优惠券抵扣金额
     */
    @TableField("coupon_money")
    @Schema(description = "优惠券抵扣金额")
    private BigDecimal couponMoney;

    /**
     * 积分抵扣金额
     */
    @TableField("points_money")
    @Schema(description = "积分抵扣金额")
    private BigDecimal pointsMoney;

    /**
     * 积分抵扣数量
     */
    @TableField("points_num")
    @Schema(description = "积分抵扣数量")
    private Integer pointsNum;

    /**
     * 实际付款金额(包含运费)
     */
    @TableField("pay_price")
    @Schema(description = "实际付款金额(包含运费)")
    private BigDecimal payPrice;

    /**
     * 后台修改的订单金额（差价）
     */
    @TableField("update_price")
    @Schema(description = "后台修改的订单金额（差价）")
    private BigDecimal updatePrice;

    /**
     * 买家留言
     */
    @TableField("buyer_remark")
    @Schema(description = "买家留言")
    private String buyerRemark;

    /**
     * 支付方式(10余额支付 20微信支付)
     */
    @TableField("pay_type")
    @Schema(description = "支付方式(10余额支付 20微信支付)")
    private Integer payType;

    /**
     * 付款状态(10未付款 20已付款)
     */
    @TableField("pay_status")
    @Schema(description = "付款状态(10未付款 20已付款)")
    private Integer payStatus;

    /**
     * 付款时间
     */
    @TableField("pay_time")
    @Schema(description = "付款时间")
    private Long payTime;

    /**
     * 配送方式(10快递配送 20上门自提)
     */
    @TableField("delivery_type")
    @Schema(description = "配送方式(10快递配送 20上门自提)")
    private Integer deliveryType;

    /**
     * 自提门店ID
     */
    @TableField("extract_shop_id")
    @Schema(description = "自提门店ID")
    private Integer extractShopId;

    /**
     * 核销店员ID
     */
    @TableField("extract_clerk_id")
    @Schema(description = "核销店员ID")
    private Integer extractClerkId;

    /**
     * 运费金额
     */
    @TableField("express_price")
    @Schema(description = "运费金额")
    private BigDecimal expressPrice;

    /**
     * 物流公司ID
     */
    @TableField("express_id")
    @Schema(description = "物流公司ID")
    private Integer expressId;

    /**
     * 物流公司
     */
    @TableField("express_company")
    @Schema(description = "物流公司")
    private String expressCompany;

    /**
     * 物流单号
     */
    @TableField("express_no")
    @Schema(description = "物流单号")
    private String expressNo;

    /**
     * 发货状态(10未发货 20已发货 30部分发货)
     */
    @TableField("delivery_status")
    @Schema(description = "发货状态(10未发货 20已发货 30部分发货)")
    private Integer deliveryStatus;

    /**
     * 发货时间
     */
    @TableField("delivery_time")
    @Schema(description = "发货时间")
    private Long deliveryTime;

    /**
     * 收货状态(10未收货 20已收货)
     */
    @TableField("receipt_status")
    @Schema(description = "收货状态(10未收货 20已收货)")
    private Integer receiptStatus;

    /**
     * 收货时间
     */
    @TableField("receipt_time")
    @Schema(description = "收货时间")
    private Long receiptTime;

    /**
     * 订单状态(10进行中 20取消 21待取消 30已完成)
     */
    @TableField("order_status")
    @Schema(description = "订单状态(10进行中 20取消 21待取消 30已完成)")
    private Integer orderStatus;

    /**
     * 审核状态（0待审核，10已驳回，20已通过，30初审待审核，40初审已驳回，50初审已通过）
     */
    @TableField("audit_status")
    @Schema(description = "审核状态（0待审核，10已驳回，20已通过，30初审待审核，40初审已驳回，50初审已通过）")
    private Integer auditStatus;

    /**
     * 审核时间
     */
    @TableField("audit_time")
    @Schema(description = "审核时间")
    private Long auditTime;

    /**
     * 驳回原因
     */
    @TableField("reject_reason")
    @Schema(description = "驳回原因")
    private String rejectReason;

    /**
     * 赠送的积分数量
     */
    @TableField("points_bonus")
    @Schema(description = "赠送的积分数量")
    private Integer pointsBonus;

    /**
     * 订单是否已结算(0未结算 1已结算)
     */
    @TableField("is_settled")
    @Schema(description = "订单是否已结算(0未结算 1已结算)")
    private Integer isSettled;

    /**
     * 微信支付交易号
     */
    @TableField("transaction_id")
    @Schema(description = "微信支付交易号")
    private String transactionId;

    /**
     * 是否已评价(0否 1是)
     */
    @TableField("is_comment")
    @Schema(description = "是否已评价(0否 1是)")
    private Integer isComment;

    /**
     * 订单来源(10普通订单 20砍价订单 30秒杀订单)
     */
    @TableField("order_source")
    @Schema(description = "订单来源(10普通订单 20砍价订单 30秒杀订单)")
    private Integer orderSource;

    /**
     * 来源记录ID
     */
    @TableField("order_source_id")
    @Schema(description = "来源记录ID")
    private Integer orderSourceId;

    /**
     * 用户ID
     */
    @TableField("user_id")
    @Schema(description = "用户ID")
    private Integer userId;

    /**
     * 是否删除
     */
    @TableField("is_delete")
    @Schema(description = "是否删除")
    @TableLogic
    private Integer isDelete;

    /**
     * 小程序ID
     */
    @TableField("wxapp_id")
    @Schema(description = "小程序ID")
    private Integer wxappId;

    /**
     * 创建时间
     */
    @TableField("create_time")
    @Schema(description = "创建时间")
    private Long createTime;

    /**
     * 更新时间
     */
    @TableField("update_time")
    @Schema(description = "更新时间")
    private Long updateTime;

    // ========== 非数据库字段 ==========

    /**
     * 订单状态文本
     */
    @TableField(exist = false)
    @Schema(description = "订单状态文本")
    private String stateText;

    /**
     * 订单商品
     */
    @TableField(exist = false)
    @Schema(description = "订单商品")
    private List<OrderGoods> goods;

    /**
     * 订单地址
     */
    @TableField(exist = false)
    @Schema(description = "订单地址")
    private OrderAddress address;

    /**
     * 自提信息
     */
    @TableField(exist = false)
    @Schema(description = "自提信息")
    private OrderExtract extract;

    /**
     * 用户信息
     */
    @TableField(exist = false)
    @Schema(description = "用户信息")
    private User user;

    /**
     * 物流信息
     */
    @TableField(exist = false)
    @Schema(description = "物流信息")
    private Express express;

    /**
     * 获取订单价格
     */
    public BigDecimal getOrderPrice() {
        if (orderPrice != null && orderPrice.compareTo(BigDecimal.ZERO) == 0) {
            return totalPrice.subtract(couponMoney != null ? couponMoney : BigDecimal.ZERO)
                    .add(updatePrice != null ? updatePrice : BigDecimal.ZERO);
        }
        return orderPrice;
    }

    /**
     * 生成订单号
     */
    public String generateOrderNo() {
        // 生成订单号逻辑
        return "ORDER" + System.currentTimeMillis() + String.format("%04d", (int)(Math.random() * 10000));
    }

    /**
     * 获取订单状态文本
     */
    public String getStateText() {
        // 审核状态
        if ((auditStatus == 0 || auditStatus == 30) && orderStatus == 10) {
            return "审核中";
        } else if ((auditStatus == 10 || auditStatus == 40) && orderStatus == 10) {
            return "驳回";
        }

        // 取消申请
        if (orderStatus == 21) {
            return "取消申请中";
        }

        // 订单状态
        if (orderStatus == 20) {
            return "已取消";
        } else if (orderStatus == 30) {
            return "已完成";
        }

        // 支付状态
        if (payStatus == 10) {
            return "待付款";
        }

        // 配送状态
        if (deliveryStatus == 10) {
            return "已付款，待发货";
        } else if (deliveryStatus == 30) {
            return "待收货(部分发货)";
        } else if (deliveryStatus == 20) {
            return "待收货(全部发货)";
        }

        // 收货状态
        if (receiptStatus == 10) {
            return "已发货，待收货";
        }

        return "";
    }

    /**
     * 判断订单是否已完成
     */
    public boolean isCompleted() {
        return orderStatus != null && orderStatus == 30;
    }

    /**
     * 判断订单是否已支付
     */
    public boolean isPaid() {
        return payStatus != null && payStatus == 20;
    }

    /**
     * 判断订单是否已发货
     */
    public boolean isDelivered() {
        return deliveryStatus != null && (deliveryStatus == 20 || deliveryStatus == 30);
    }

    /**
     * 判断订单是否已收货
     */
    public boolean isReceived() {
        return receiptStatus != null && receiptStatus == 20;
    }
} 